package com.yanshaohui.configuations;

import com.yanshaohui.bean.Person;
import com.yanshaohui.configuations.reactiveRedis.PersonRedisTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * Created by TS-Ysh on 2018-05-14.
 */
@Configuration
public class ReactiveRedisConfig {

    /**
     * 	https://github.com/spring-projects/spring-data-examples/blob/master/redis/reactive/src/test/java/example/springdata/redis/RedisTestConfiguration.java
     * 	强类型的缓存，实践中可以根据各种类型实现多种缓存类型的ReactiveRedisTemplate，分别注入使用(如下个方法)
     * @param factory
     * @return
     */
    @Bean
    public PersonRedisTemplate reactivePersonRedisTemplate(ReactiveRedisConnectionFactory factory) {
        RedisSerializationContext.RedisSerializationContextBuilder<String, Person> builder = RedisSerializationContext
                .newSerializationContext(new StringRedisSerializer());
        RedisSerializationContext<String, Person> serializationContext = builder.value(new Jackson2JsonRedisSerializer(Person.class)).build();
        return new PersonRedisTemplate(factory, serializationContext);
    }


//	@Bean
//	public ObjectMapper objectMapper(){
//		return new ObjectMapper();
//	}
//
//	@Bean
//	public ReactiveRedisTemplate<String, Object> reactiveRedisTemplate2(ReactiveRedisConnectionFactory factory,ObjectMapper mapper) {
//		RedisSerializationContext.RedisSerializationContextBuilder<String, Object> builder = RedisSerializationContext
//				.newSerializationContext(new StringRedisSerializer());
//
//		RedisSerializationContext<String, Object> serializationContext = builder
//				.value(new GenericJackson2JsonRedisSerializer(mapper)).build();
//
//		return new ReactiveRedisTemplate<>(factory, serializationContext);
//	}
}
